package com.dizner.diassistant.database

import android.util.Log
import androidx.room.EmptyResultSetException
import com.dizner.diassistant.dataentry.BloodPressureHistoryEntry
import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*
import kotlin.collections.ArrayList

class DBOptionManager {


    private val TAG: String = "DBOptionManager"

    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder = DBOptionManager()
    }

    /**
     * 处理数据库查询返回的Single结果
     * 有结果调用onSuccess，空结果或查询出错调用onFailure
     */
    private fun <T> Single<T>.subscribeDBResult(callback: Callback<T>?) {
        subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : SingleObserver<T> {
                override fun onSuccess(t: T) {
                    Log.d(TAG, "operate db success----data:$t")
                    callback?.onSuccess(t)
                }

                override fun onSubscribe(d: Disposable) {
                    callback?.onStart()
                }

                override fun onError(e: Throwable) {
                    Log.d(TAG, "operate db failure----error:$e")
                    if (e is EmptyResultSetException) {
                        callback?.onSuccess(null)
                        return
                    }
                    callback?.onFailure(e)
                }
            })
    }

    /**
     * 数据库CRUD操作
     */
    fun <T> executeDBOption(data: Single<T>, callback: Callback<T>?) {
        data.subscribeDBResult(callback)
    }

    open abstract class Callback<T> {
        open fun onStart() {}
        abstract fun onSuccess(data: T?)
        abstract fun onFailure(error: Throwable)
    }

    fun initData(): List<BloodPressureHistoryEntry> {
        var datas = ArrayList<BloodPressureHistoryEntry>()

        datas.add(BloodPressureHistoryEntry(136, 91, 87, "2019-11-14 20:00:00"))
        datas.add(BloodPressureHistoryEntry(124, 92, 78, "2019-11-14 20:02:00"))
        datas.add(BloodPressureHistoryEntry(131, 93, 80, "2019-11-14 20:04:00"))

        datas.add(BloodPressureHistoryEntry(131, 82, 87, "2019-11-15 08:00:00"))
        datas.add(BloodPressureHistoryEntry(137, 92, 89, "2019-11-15 08:02:00"))
        datas.add(BloodPressureHistoryEntry(141, 91, 86, "2019-11-15 08:04:00"))
        datas.add(BloodPressureHistoryEntry(136, 91, 87, "2019-11-15 20:00:00"))
        datas.add(BloodPressureHistoryEntry(124, 92, 78, "2019-11-15 20:02:00"))
        datas.add(BloodPressureHistoryEntry(131, 93, 80, "2019-11-15 20:04:00"))

        datas.add(BloodPressureHistoryEntry(130, 94, 76, "2019-11-16 08:00:00"))
        datas.add(BloodPressureHistoryEntry(134, 94, 80, "2019-11-16 08:02:00"))
        datas.add(BloodPressureHistoryEntry(127, 91, 75, "2019-11-16 08:04:00"))
        datas.add(BloodPressureHistoryEntry(141, 90, 71, "2019-11-16 20:00:00"))
        datas.add(BloodPressureHistoryEntry(132, 92, 79, "2019-11-16 20:02:00"))
        datas.add(BloodPressureHistoryEntry(143, 98, 79, "2019-11-16 20:04:00"))

        datas.add(BloodPressureHistoryEntry(130, 85, 76, "2019-11-17 08:00:00"))
        datas.add(BloodPressureHistoryEntry(126, 81, 75, "2019-11-17 08:02:00"))
        datas.add(BloodPressureHistoryEntry(127, 83, 75, "2019-11-17 08:04:00"))
        datas.add(BloodPressureHistoryEntry(129, 97, 67, "2019-11-17 20:00:00"))
        datas.add(BloodPressureHistoryEntry(121, 93, 71, "2019-11-17 20:02:00"))
        datas.add(BloodPressureHistoryEntry(131, 94, 65, "2019-11-17 20:04:00"))

        datas.add(BloodPressureHistoryEntry(137, 98, 79, "2019-11-18 08:00:00"))
        datas.add(BloodPressureHistoryEntry(140, 90, 79, "2019-11-18 08:02:00"))
        datas.add(BloodPressureHistoryEntry(127, 94, 62, "2019-11-18 08:04:00"))
        datas.add(BloodPressureHistoryEntry(139, 89, 77, "2019-11-18 20:00:00"))
        datas.add(BloodPressureHistoryEntry(133, 83, 73, "2019-11-18 20:02:00"))
        datas.add(BloodPressureHistoryEntry(134, 90, 77, "2019-11-18 20:04:00"))

        datas.add(BloodPressureHistoryEntry(139, 92, 82, "2019-11-19 08:00:00"))
        datas.add(BloodPressureHistoryEntry(146, 95, 80, "2019-11-19 08:02:00"))
        datas.add(BloodPressureHistoryEntry(138, 93, 77, "2019-11-19 08:04:00"))
        datas.add(BloodPressureHistoryEntry(128, 90, 71, "2019-11-19 20:00:00"))
        datas.add(BloodPressureHistoryEntry(125, 85, 73, "2019-11-19 20:02:00"))
        datas.add(BloodPressureHistoryEntry(119, 88, 71, "2019-11-19 20:04:00"))

        datas.add(BloodPressureHistoryEntry(128, 84, 90, "2019-11-20 08:00:00"))
        datas.add(BloodPressureHistoryEntry(141, 84, 86, "2019-11-20 08:02:00"))
        datas.add(BloodPressureHistoryEntry(132, 73, 88, "2019-11-20 08:04:00"))
        datas.add(BloodPressureHistoryEntry(134, 91, 73, "2019-11-20 20:00:00"))
        datas.add(BloodPressureHistoryEntry(132, 88, 69, "2019-11-20 20:02:00"))
        datas.add(BloodPressureHistoryEntry(133, 90, 76, "2019-11-20 20:04:00"))

        datas.add(BloodPressureHistoryEntry(141, 94, 70, "2019-11-21 20:00:00"))
        datas.add(BloodPressureHistoryEntry(132, 91, 63, "2019-11-21 20:02:00"))
        datas.add(BloodPressureHistoryEntry(139, 94, 68, "2019-11-21 20:04:00"))

        datas.add(BloodPressureHistoryEntry(138, 88, 84, "2019-11-22 08:00:00"))
        datas.add(BloodPressureHistoryEntry(131, 84, 77, "2019-11-22 08:02:00"))
        datas.add(BloodPressureHistoryEntry(127, 84, 80, "2019-11-22 08:04:00"))
        datas.add(BloodPressureHistoryEntry(136, 83, 81, "2019-11-22 20:00:00"))
        datas.add(BloodPressureHistoryEntry(131, 87, 82, "2019-11-22 20:02:00"))
        datas.add(BloodPressureHistoryEntry(132, 90, 75, "2019-11-22 20:04:00"))

        datas.add(BloodPressureHistoryEntry(132, 112, 95, "2019-11-23 08:00:00"))
        datas.add(BloodPressureHistoryEntry(133, 87, 80, "2019-11-23 08:02:00"))
        datas.add(BloodPressureHistoryEntry(130, 86, 84, "2019-11-23 08:04:00"))
        datas.add(BloodPressureHistoryEntry(136, 98, 72, "2019-11-23 20:00:00"))
        datas.add(BloodPressureHistoryEntry(138, 98, 71, "2019-11-23 20:02:00"))
        datas.add(BloodPressureHistoryEntry(139, 92, 65, "2019-11-23 20:04:00"))

        datas.add(BloodPressureHistoryEntry(133, 90, 79, "2019-11-24 08:00:00"))
        datas.add(BloodPressureHistoryEntry(130, 89, 75, "2019-11-24 08:02:00"))
        datas.add(BloodPressureHistoryEntry(127, 87, 87, "2019-11-24 08:04:00"))
        datas.add(BloodPressureHistoryEntry(142, 98, 76, "2019-11-24 20:00:00"))
        datas.add(BloodPressureHistoryEntry(142, 99, 74, "2019-11-24 20:02:00"))
        datas.add(BloodPressureHistoryEntry(140, 97, 78, "2019-11-24 20:04:00"))

        datas.add(BloodPressureHistoryEntry(145, 87, 87, "2019-11-25 08:00:00"))
        datas.add(BloodPressureHistoryEntry(141, 89, 85, "2019-11-25 08:02:00"))
        datas.add(BloodPressureHistoryEntry(125, 88, 83, "2019-11-25 08:04:00"))
        datas.add(BloodPressureHistoryEntry(131, 95, 71, "2019-11-25 20:00:00"))
        datas.add(BloodPressureHistoryEntry(128, 90, 73, "2019-11-25 20:02:00"))
        datas.add(BloodPressureHistoryEntry(133, 89, 72, "2019-11-25 20:04:00"))

        datas.add(BloodPressureHistoryEntry(130, 87, 78, "2019-11-26 08:00:00"))
        datas.add(BloodPressureHistoryEntry(128, 89, 75, "2019-11-26 08:02:00"))
        datas.add(BloodPressureHistoryEntry(128, 90, 85, "2019-11-26 08:04:00"))
        datas.add(BloodPressureHistoryEntry(125, 94, 71, "2019-11-26 20:00:00"))
        datas.add(BloodPressureHistoryEntry(130, 93, 71, "2019-11-26 20:02:00"))
        datas.add(BloodPressureHistoryEntry(129, 92, 73, "2019-11-26 20:04:00"))

        datas.add(BloodPressureHistoryEntry(118, 89, 88, "2019-11-27 08:00:00"))
        datas.add(BloodPressureHistoryEntry(133, 84, 84, "2019-11-27 08:02:00"))
        datas.add(BloodPressureHistoryEntry(131, 79, 84, "2019-11-27 08:04:00"))
        datas.add(BloodPressureHistoryEntry(140, 102, 62, "2019-11-27 20:00:00"))
        datas.add(BloodPressureHistoryEntry(140, 98, 66, "2019-11-27 20:02:00"))
        datas.add(BloodPressureHistoryEntry(139, 97, 61, "2019-11-27 20:04:00"))


        return datas

    }
}